package cn.hdustea.shopNavigation.widget.scanner

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import cn.hdustea.shopNavigation.callback.CallBackManager
import cn.hdustea.shopNavigation.callback.CallBackType
import cn.hdustea.shopNavigation.callback.IGlobalCallback
import me.dm7.barcodescanner.zbar.Result
import me.dm7.barcodescanner.zbar.ZBarScannerView.ResultHandler

/**
 * Created by HuQiang on 2017/11/23.
 */
class ScanActivity : AppCompatActivity(), ResultHandler {
    private var mScanView: ScanView? = null

    companion object {

        fun startActivity(context: Context) {
            context.startActivity(Intent(context, ScanActivity::class.java))
        }

    }

    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (mScanView == null) {
            mScanView = ScanView(this)
        }
        setContentView(mScanView)
        mScanView!!.setAutoFocus(true)
        mScanView!!.setResultHandler(this)
    }

    public override fun onResume() {
        super.onResume()
        if (mScanView != null) {
            mScanView!!.startCamera()
        }
    }

    public override fun onPause() {
        super.onPause()
        if (mScanView != null) {
            mScanView!!.stopCameraPreview()
            mScanView!!.stopCamera()
        }
    }

    override fun handleResult(result: Result) {
        val callback = CallBackManager
                .getInstance().getCallBack(CallBackType.ON_SCAN)
        callback?.executeCallBack(result.contents)
        finish()
    }
}